package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsSequenceSection;
import net.realsee.domain.wcs.data.bo.WcsSequenceSectionBo;
import net.realsee.domain.wcs.data.vo.WcsSequenceSectionVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.data.WcsSequenceSectionMapper;
import net.realsee.service.wcs.data.IWcsSequenceSectionService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 序列号子-序列号段Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsSequenceSectionServiceImpl extends ServiceImpl<WcsSequenceSectionMapper, WcsSequenceSection> implements IWcsSequenceSectionService {

    private final WcsSequenceSectionMapper baseMapper;

    /**
     * 查询序列号子-序列号段
     */
    @Override
    public WcsSequenceSectionVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询序列号子-序列号段列表
     */
    @Override
    public TableDataInfo<WcsSequenceSectionVo> selectVoPage(WcsSequenceSectionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsSequenceSection> lqw = buildVoQueryWrapper(bo);
        Page<WcsSequenceSectionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询序列号子-序列号段列表
     */
    @Override
    public List<WcsSequenceSectionVo> selectVoList(WcsSequenceSectionBo bo) {
        LambdaQueryWrapper<WcsSequenceSection> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsSequenceSection> buildVoQueryWrapper(WcsSequenceSectionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsSequenceSection> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsSequenceSection::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getFacility()), WcsSequenceSection::getFacility, bo.getFacility());
        lqw.eq(StringUtils.isNotBlank(bo.getSequence()), WcsSequenceSection::getSequence, bo.getSequence());
        lqw.eq(StringUtils.isNotBlank(bo.getSectionCode()), WcsSequenceSection::getSectionCode, bo.getSectionCode());
        lqw.eq(bo.getSeqNo() != null, WcsSequenceSection::getSeqNo, bo.getSeqNo());
        lqw.eq(StringUtils.isNotBlank(bo.getSectionType()), WcsSequenceSection::getSectionType, bo.getSectionType());
        lqw.eq(StringUtils.isNotBlank(bo.getPattern()), WcsSequenceSection::getPattern, bo.getPattern());
        lqw.eq(bo.getLength() != null, WcsSequenceSection::getLength, bo.getLength());
        lqw.eq(StringUtils.isNotBlank(bo.getAlign()), WcsSequenceSection::getAlign, bo.getAlign());
        lqw.eq(StringUtils.isNotBlank(bo.getPaddingChar()), WcsSequenceSection::getPaddingChar, bo.getPaddingChar());
        lqw.eq(bo.getStep() != null, WcsSequenceSection::getStep, bo.getStep());
        lqw.eq(bo.getMin() != null, WcsSequenceSection::getMin, bo.getMin());
        lqw.eq(bo.getCurrent() != null, WcsSequenceSection::getCurrent, bo.getCurrent());
        lqw.eq(bo.getMax() != null, WcsSequenceSection::getMax, bo.getMax());
        lqw.eq(bo.getIsCycled() != null, WcsSequenceSection::getIsCycled, bo.getIsCycled());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsSequenceSection::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsSequenceSection::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsSequenceSection::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsSequenceSection::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsSequenceSection::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsSequenceSection::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsSequenceSection::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsSequenceSection::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsSequenceSection::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsSequenceSection::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsSequenceSection::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsSequenceSection::getRowVersionStamp, bo.getRowVersionStamp());
        return lqw;
    }

    /**
     * 新增序列号子-序列号段
     */
    @Override
    public Boolean insertByBo(WcsSequenceSectionBo bo) {
        WcsSequenceSection add = BeanUtil.toBean(bo, WcsSequenceSection.class);
        return this.save(add);
    }

    /**
     * 修改序列号子-序列号段
     */
    @Override
    public Boolean updateByBo(WcsSequenceSectionBo bo) {
        WcsSequenceSection update = BeanUtil.toBean(bo, WcsSequenceSection.class);
        return this.updateById(update);
    }

    /**
     * 批量删除序列号子-序列号段
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
